/*
加载js时创建长连接
*/
if (window.WebSocket) {
  console.log('浏览器支持websocket');
} else {
  console.log('浏览器不支持websocket');
}
var wsPath = 'ws://127.0.0.1:13426';
var ws = new WebSocket(wsPath);

ws.onopen = function (event) {
  console.info('websocket长连接成功...' + wsPath);
  // console.log(ws.readyState+"  "+ws.closed);
};
ws.onclose = function (event) {
  console.info('websocket关闭...' + wsPath);
};
ws.onerror = function (event) {
  console.info('websocket异常...' + wsPath);
};

var nativeWebsocket = async function (obj) {
  // console.log("cs:"+ws.readyState);
  var cmddata = obj.data; //发送数据
  //console.log("cmddata:"+cmddata);
  var wsPath = obj.url; //链接地址  保留
  var listenFlag = obj.listenFlag; //监听开关
  var CallBack = obj.CallBack; //回调函数
  let isAsync = typeof CallBack == 'function'; //判断回调函数是不是方法
  if (listenFlag) {
    //事件监听
    return new Promise((resolve, reject) => {
      var wsLisPath = 'ws://127.0.0.1:13426';
      var wsLis = new WebSocket(wsLisPath);
      wsLis.onopen = function (event) {
        // console.info("websocket-lis连接成功..." + wsLisPath);
        wsLis.send(cmddata);
      };
      wsLis.onmessage = function (event) {
        console.info('websocket-lis接收数据：' + event.data);
        if (isAsync) {
          // 调用回调方法
          CallBack(event.data);
          wsLis.close(); //成功关闭当前链接
        }
        //	opneListenEventCallback(event.data);
      };
      wsLis.onclose = function (event) {
        // console.info("websocket-lis关闭..." + wsLisPath);
      };
      wsLis.onerror = function (event) {
        console.info('websocket-lis异常...' + wsLisPath);
        reject(event);
      };
    });
  } else {
    //无监听分支

    if (isAsync) {
      //异步模式，判断是不是传了回调函数，判断回调函数是不是方法
      var ws2 = new WebSocket('ws://127.0.0.1:13426');
      ws2.onopen = function (event) {
        ws2.send(cmddata);
      };

      ws2.onmessage = function (event) {
        // console.info("websocket-异步接收数据：" + event.data);
        // 调用回调方法
        CallBack(event.data);
        ws2.close(); //成功关闭当前链接
      };
      ws2.onclose = function (event) {
        // console.info("websocket异步关闭...");
      };
      ws2.onerror = function (event) {
        console.info('websocket异步异常...');
      };
    } else {
      //同步模式

      if (ws.readyState == 3) {
        //链接已关闭没有连接成功
        ws = new WebSocket(wsPath);

        return new Promise((resolve, reject) => {
          ws.onopen = function (event) {
            // console.info("websocket异常修复连接成功..." + wsPath);
            // console.log("异常修复连接3"+cmddata);
            ws.send(cmddata);
          };
          ws.onmessage = function (event) {
            console.info('websocket异常修复接接收数据：' + event.data);
            resolve(event.data); //通过promise对象返回接口同步数据
          };
          ws.onclose = function (event) {
            // console.info("websocket关闭..." + wsPath);
          };
          ws.onerror = function (event) {
            console.info('websocket异常...' + wsPath);
          };
        });
      } else if (ws.readyState == 0) {
        // console.log("正在链接中");
        var wsconnecting = new WebSocket('ws://127.0.0.1:13426');

        return new Promise((resolve, reject) => {
          wsconnecting.onopen = function (event) {
            wsconnecting.send(cmddata);
          };
          wsconnecting.onmessage = function (event) {
            // console.info("websocket-连接中-接收数据：" + event.data);
            resolve(event.data); //通过promise对象返回接口同步数据
            //wsconnecting.close(); //成功关闭当前链接
          };
          wsconnecting.onclose = function (event) {
            // console.info("websocket链接中关闭...");
          };
          wsconnecting.onerror = function (event) {
            console.info('websocket链接中异常...');
          };
        });
      } else if (ws.readyState == 2) {
        // console.log("连接正在关闭");
      } else {
        //正常逻辑
        // console.log("正常连接");
        return new Promise((resolve, reject) => {
          ws.send(cmddata);
          ws.onmessage = function (event) {
            console.info('websocket长连接接收数据：' + event.data);
            resolve(event.data); //通过promise对象返回接口同步数据
          };
        });
      }

      //同步模式结束
    }
  }
};

export default nativeWebsocket;
